#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <sys/shm.h>
#include <cassert>
#include <fcntl.h>

using namespace std;

#define PATH_NAME "/home/hyr/linux_code"
#define PROJ_ID 0x14
#define MEM_SIZE 4096
#define FIFO_FILE ".fifo"

// 创建一个全新的共享内存
const int flag = IPC_CREAT | IPC_EXCL | 0666;

key_t CreateKey()
{
    key_t key = ftok(PATH_NAME, PROJ_ID);
    if (key < 0)
    {
        cerr << "ftok : " << strerror(errno) << endl;
        exit(-1);
    }
    return key;
}

std::ostream &Log()
{
    std::cout << "For ebug |  " << " timestamp: " << (uint64_t)time(nullptr) << " | ";
    return std::cout;
}

void CreateFifo()
{
    umask(0);
    if (mkfifo(FIFO_FILE, 0666) < 0)
    {
        Log() << strerror(errno) << endl;
        exit(2);
    }
}

#define READER O_RDONLY
#define WRITER O_WRONLY

int Open(const string &filename, int flags)
{
    return open(filename.c_str(), flags);
}

int Wait(int fd)
{
    uint32_t values = 0;
    ssize_t s = read(fd, &values, sizeof(values));
    return s;
}

int Signal(int fd)
{
    uint32_t cmd = 1;
    write(fd, &cmd, sizeof(cmd));
}

int Close(int fd, const string filename)
{
    close(fd);
    unlink(filename.c_str());
}
